package com.plant.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plant.entity.UgcFollow;
import com.plant.entity.UgcFriends;
import com.plant.entity.UgcUser;
import com.plant.service.FollowUserService;
import com.plant.service.HelpService;
/**
 * 	涉及的表：表1：ugc_user；author TOMlee
 */
@Controller
@RequestMapping(value = "/follow")
public class FollowUserController {

	@Resource(name="followUserServiceImpl")
	private FollowUserService followUserService;

	@Resource(name = "helpServiceImpl")
	private HelpService helpService;

	@RequestMapping(value="five_userinf")//----推荐五个普通用户
	@ResponseBody
	public Map<String, Object> FindFiveUserInf(UgcUser user,Integer current_user_id,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null||current_user_id==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				String level="1";
				user.setId(current_user_id);
				List<Object> list3=new ArrayList<Object>();
				List<Object> list2=new ArrayList<Object>();
				list2=helpService.GetFriendUidByA(user);
				list3=helpService.GetFriendUidByR(user);
				list3.addAll(list2);
				list3.add(current_user_id);
				Object ids=StringUtils.join(list3.toArray(),",");
				List<Map<String,Object>> fiveuserinf=new ArrayList<Map<String,Object>>();
				fiveuserinf=followUserService.LookFiveUserinf(ids,level);
				if(fiveuserinf.size()>0){
					map.put("userinf", fiveuserinf);
				}else{
					map.put("userinf", "没有更多用户推荐");
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}


	@RequestMapping(value="five_approve_userinf")//--3.推荐五个认证用户
	@ResponseBody
	public Map<String, Object> FiveApproveUserinf(UgcUser user,Integer current_user_id,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null||current_user_id==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				String level="2";
				user.setId(current_user_id);
				List<Object> list3=new ArrayList<Object>();
				list3=followUserService.GetFollowUserByApply(user);
				list3.add(current_user_id);
				Object ids=StringUtils.join(list3.toArray(),",");
				List<Map<String,Object>> fiveuserinf=new ArrayList<Map<String,Object>>();
				fiveuserinf=followUserService.LookFiveUserinf(ids,level);
				if(fiveuserinf.size()>0){
					map.put("userinf", fiveuserinf);
				}else{
					map.put("userinf", "没有更多用户推荐");
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}


	@RequestMapping(value="add_official")//4.添加官方账号为好友
	@ResponseBody
	public Map<String, Object> AddOfficial(UgcFriends uf,Integer current_user_id,HttpServletRequest request,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null||current_user_id==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				Integer receive_user_id=154;
				uf.setReceiveUserId(receive_user_id);
				uf.setApplyUserId(current_user_id);
				boolean bb=followUserService.CheckOfficalAccount(uf);
				if(bb){
					map.put("message", "官方账号已经存在");
				}else{
					SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String time=sim.format(new Date());
					GetIp getip=new GetIp();
					String ip=getip.getIp2(request);
					uf.setCreateTime(Timestamp.valueOf(time));
					uf.setIp(ip);
					boolean b=followUserService.AddOfficialAccountFriend(uf);
					if(b){
						map.put("message", "添加官方账号成功");
						map.put("state", "1");
					}else{
						map.put("message", "添加官方账号失败");
						map.put("state", "0");
					}
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}


	@RequestMapping(value="follow_approveuser")//订阅认证用户
	@ResponseBody
	public Map<String, Object> FollowApproveuser(Integer receive_user_id,Integer apply_user_id,HttpServletRequest request,UgcFollow ufo,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time=sim.format(new Date());
				GetIp getip=new GetIp();
				String ip=getip.getIp2(request);
				ufo.setApplyUserId(apply_user_id);
				ufo.setCreateTime(Timestamp.valueOf(time));
				ufo.setReceiveUserId(receive_user_id);
				ufo.setIp(ip);
				boolean b=followUserService.AddFollowUser(ufo);
				if(b){
					map.put("message", "订阅用户成功");
					map.put("state", "1");
				}else{
					map.put("message", "订阅用户失败");
					map.put("state", "0");
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}



	@RequestMapping(value="show_official_account")//显示官方账号
	@ResponseBody
	public Map<String, Object> ShowOfficialAccount(UgcUser user,String secret,String pust_val){
		Map<String,Object> map=new HashMap<String, Object>();
		if(secret==null||pust_val==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				user.setId(154);
				List<Map<String, Object>> officiakAccount=new ArrayList<Map<String,Object>>();
				officiakAccount=followUserService.GetOfficialAccount(user);
				if(officiakAccount.size()>0){
					map.put("officiakAccount", officiakAccount);
				}else{
					map.put("message", "官方账号正在更新哦");
					map.put("state", "0");
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	}


}
